var mecsArray = new Array("KT-I1","KT-I2","KT-II1","KT-II2");
var list = new Array;


//获取当前网址，如： http://localhost:8080/Tmall/index.jsp 
var curWwwPath=window.document.location.href; 
//获取主机地址之后的目录如：/Tmall/index.jsp 
var pathName=window.document.location.pathname; 
var pos=curWwwPath.indexOf(pathName); 
//获取主机地址，如： http://localhost:8080 
var localhostPath=curWwwPath.substring(0,pos); 
var targetPath = localhostPath.replace("8080","10080");

//获取风机数据

function getMecsStatus(){
	$.ajax({
		type:'post',
		url: targetPath + '/rest/getData?start=v30&end=v89',
		dataType: 'json',//here
		success: function(result)
		{
			if(result.code=="200"){
				//现将原数组清空
				list = [];
				var data = result.data;
				if(data.length>0){
					var index = 0;
					for(var i=0;i<data.length;i++){
						if(i==0 || i%15==0){
							var object = new Object();
							object.ID = mecsArray[index];
							object.S02 = data[i];
							object.S01 = data[i+1];
							object.S20 = data[i+2];
							object.S17 = data[i+3];
							object.A01 = data[i+4];
							object.A02 = data[i+5];
							object.A03 = data[i+6];
							object.N01 = data[i+7];
							object.T02 = data[i+8];
							object.T03 = data[i+9];
							object.T04 = data[i+10];
							object.T05 = data[i+11];
							object.D02 = data[i+12];
							object.C02 = data[i+13];
							object.C01 = data[i+14];
							list.push(object);
							index++;
						}
						
					}
				}
				if(list.length>0){
					for(var i=0;i<list.length;i++){
						var id = list[i].ID;
						var s02 = list[i].S02;
						var s01 = list[i].S01;
						var s20 = list[i].S20;
						var s17 = list[i].S17;
						var a01 = list[i].A01;
						var a02 = list[i].A02;
						var a03 = list[i].A03;
						var n01 = list[i].N01;
						var t02 = list[i].T02;
						var t03 = list[i].T03;
						var t04 = list[i].T04;
						var t05 = list[i].T05;
						var d02 = list[i].D02;
						var c02 = list[i].C02;
						var c01 = list[i].C01;
//						alert(id+","+s02+","+s01+","+s20+","+s17+","+a01+","+a02+","
//								+a03+","+n01+","+t02+","+t03+","+t04+","+t05+","+d02+","+c02+","+c01);
						//判断状态
						if(s02 == '1' && s01 == '0' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0) 
							&& s17 == '1'){
							//绿变
							$("#"+id).attr("src","static/images/fengji/da/ZKT_green.gif");
							$("#TxtEmcs"+id).html('变');
							$("#TxtDiv"+id).css("color","#4EA01B");
						}else if(s02 == '1' && s01 == '0' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0) 
							&& s17 == '0'){
							//绿工
							$("#"+id).attr("src","static/images/fengji/da/ZKT_green.gif");
							$("#TxtEmcs"+id).html('工');
							$("#TxtDiv"+id).css("color","#4EA01B");
						}else if(s02 == '0' && s01 == '1' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0) 
							){
							//灰色
							$("#"+id).attr("src","static/images/fengji/da/ZKT_grey.png");
							$("#TxtEmcs"+id).html('');
							//$("#TxtDiv"+id).attr("class","");
						}else if(s02 == '1' && s01 == '0' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0) 
							&& s17 == '1'){
							//黄变
							$("#"+id).attr("src","static/images/fengji/da/ZKT_yellow_run.gif");
							$("#TxtEmcs"+id).html('变');
							$("#TxtDiv"+id).css("color","#DFBC00");
						}else if(s02 == '1' && s01 == '0' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0) 
							&& s17 == '0'){
							//黄工
							$("#"+id).attr("src","static/images/fengji/da/ZKT_yellow_run.gif");
							$("#TxtEmcs"+id).html('工');
							$("#TxtDiv"+id).css("color","#DFBC00");
						}else if(s02 == '0' && s01 == '1' 
							&& (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0)){
							//黄停
							$("#"+id).attr("src","static/images/fengji/da/ZKT_yellow_stop.png");
							$("#TxtEmcs"+id).html('');
							//$("#TxtDiv"+id).attr("class","");
						}else if((parseInt(s01) + parseInt(s02) != 1)){
							//蓝色
							$("#"+id).attr("src","static/images/fengji/da/ZKT_blue.png");
							$("#TxtEmcs"+id).html('');
							//$("#TxtDiv"+id).attr("class","");
						}
						
						//给数据表格赋值
						if($("#zhsktq").css('display')=='block'){
							if(devcode==id){
								fuzhi(devcode);
							}
						}
						
					}
				}
			}else{
				//状态不等于200.未知状态
				if(mecsArray.length > 0){
					for(var i=0;i<mecsArray.length;i++){
						$("#"+mecsArray[i]).attr("src","static/images/fengji/da/ZKT_blue.png");
						$("#TxtEmcs"+mecsArray[i]).html('');
						$("#TxtDiv"+mecsArray[i]).attr("class","");
					}
				}
			}
			
		}
	});
	
}


var devcode="";
function show1(obj){
		if($("#dxthpfj").css('display')=='block'){
			$("#dxthpfj").hide();
		}
		$("#zhsktq").show();
		devcode = obj;
		fuzhi(devcode);
}

function hide1(){
	$("#zhsktq").hide();
	$("#plsd").val("");
}

function fuzhi(devcode){
	var a=0;
	if(null != devcode && '' != devcode){
		for(var i=0;i<list.length;i++){
			if(list[i].ID == devcode){
				a = i;
				break;
			}else if(i==(list.length-1) && list[i].ID != devcode){
				return;
			}
		}
		$("#biaotiID").html(devcode);
		$("#devID").html(devcode);
		
		if(list[a].S02=='1' && list[a].S01=='0'){//运行状态 0无，1运行
			$("#ydzt").html("运行");
		}else if(list[a].S02=='0' && list[a].S01=='1'){
			$("#ydzt").html("停止");
		}else{
			$("#ydzt").html("异常");
		}
		
		if(list[a].A01 == '0'){
			$("#gzgz").html("无");
		}else if(list[a].A01 == '1'){
			$("#gzgz").html("故障");
		}
		
		if(list[a].A02 == '0'){
			$("#bpqtxgz").html("无");
		}else if(list[a].A02 == '1'){
			$("#bpqtxgz").html("故障");
		}
		
		if(list[a].A03 == '0'){
			$("#bpqgzbj").html("无");
		}else if(list[a].A03 == '1'){
			$("#bpqgzbj").html("故障");
		}
		
		if(list[a].N01 == '0'){
			$("#ls").html("不满足");
		}else if(list[a].N01 == '1'){
			$("#ls").html("满足");
		}
		
		if(list[a].S20 == '0'){
			$("#dqczcs").html("就地");
		}else if(list[a].S20 == '1'){
			$("#dqczcs").html("远方");
		}
		
		$("#ydpl").html(list[a].T02+"HZ");
		
		$("#ljyxsj").html(list[a].T03);
		
		$("#sxpjdl").html(list[a].T04+"A");
		
		$("#ygdd").html(list[a].T05+"KWh");
	}
	
}

//运行控制
function yxkz(){
	var a = getXiabiao();
	var S20 = list[a].S20; 
	var N01 = list[a].N01;
	if(S20 == '1' && N01 == '1'){
		//输出C02=1
		var num;
		var val = "1";

		if(list[a].ID == "KT-I1"){
			num = "43";
		}else if(list[a].ID == "KT-I2"){
			num = "58";
		}else if(list[a].ID == "KT-II1"){
			num = "73";
		}else if(list[a].ID == "KT-II2"){
			num = "88";
		}else{
			alert("无该设备！");
			return;
		}
		writeData(num,val);
	}else if(S20 == '0'){
		alert("设备处于就地控制状态！");
	}else if(N01 == '0'){
		alert("设备联锁条件不满足！");
	}
	
}

//停止控制
function tzkz(){
	var a = getXiabiao();
	var S20 = list[a].S20; 
	
	if(S20=='1'){
		var num;
		var val = "1";

		if(list[a].ID == "KT-I1"){
			num = "44";
		}else if(list[a].ID == "KT-I2"){
			num = "59";
		}else if(list[a].ID == "KT-II1"){
			num = "74";
		}else if(list[a].ID == "KT-II2"){
			num = "89";
		}else{
			alert("无该设备！");
			return;
		}
		writeData(num,val);
	}else if(S20=='0'){
		alert("设备处于就地控制状态！");
	}else{
		alert("数据异常");
	}
}

//频率设定
function plsd(){
	var a = getXiabiao();
	var S20 = list[a].S20;
	var D02 = $("#plsd").val();

	if(D02 == "" || D02 == " " || D02 == undefined){
		alert("输入不能为空(频率设定范围为25~50Hz！)");
	}else if(S20 == '1' && ( parseInt(D02) >= 25 && parseInt(D02)<=50 )){
		//更改D02
		var num;
		
		//此处位置实际是T02的位置
		if(list[a].ID == "KT-I1"){
			num = "38";
		}else if(list[a].ID == "KT-I2"){
			num = "53";
		}else if(list[a].ID == "KT-II1"){
			num = "68";
		}else if(list[a].ID == "KT-II2"){
			num = "83";
		}else{
			alert("无该设备！");
			return;
		}
		
		$("#ydpl").html(D02+"HZ");
		
		writeData(num,D02);
		
	}else if(S20 == '0' && ( parseInt(D02) >=25 && parseInt(D02)<=50 )){
		alert("设备处于就地控制状态！");
	}else if(S20 == '1' && ( parseInt(D02) <25 || parseInt(D02)>50 )){
		alert("频率设定范围为25~50Hz！");
	}else{
		alert("频率设定范围为25~50Hz！");
	}
	
}

//修改数据
function writeData(num,val){
	$.ajax({
		type:'post',
		url: targetPath + '/modbus/writeData?key=v'+num+'&val='+val,
		dataType: 'json',//here
		success: function(result)
		{
			if(result.message == "write success"){
				alert("更改成功");
			}
		}
	});
}

//得到对应下标
function getXiabiao(){
	var a=0;
	var devcode = $("#devID").html();
	for(var i=0;i<list.length;i++){
		if(list[i].ID == devcode){
			a = i;
			break;
		}else if(i==(list.length-1) && list[i].ID != devcode){
			return;
		}
	}
	return a;
}

setInterval(function() {
	getMecsStatus();
}, 1000);